package com.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.dao.ExamAnswerDetailMapper;
import com.dao.TestPaperQuestionMapper;
import com.entity.ExamAnswerDetail;
import com.entity.SignTask;
import com.exception.CustomTransactionalException;
import com.form.question.QuestionPageForm;
import com.vo.PageVo;
import com.vo.signTask.DisplaySignTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.entity.TestQuestion;
import com.dao.TestQuestionMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Version: V1.0    <br/>
 * Datetime:   2022/5/15 16:35   <br/>
 * Description: ${DES}
 *
 * @author: chen
 */
@Service
public class TestQuestionService {

    @Resource
    private TestQuestionMapper testQuestionMapper;

    @Resource
    private TestPaperQuestionMapper testPaperQuestionMapper;

    @Resource
    private ExamAnswerDetailMapper examAnswerDetailMapper;


    public int deleteByPrimaryKey(Integer id) {
        return testQuestionMapper.deleteByPrimaryKey(id);
    }


    public int insert(TestQuestion record) {
        return testQuestionMapper.insert(record);
    }


    public int insertSelective(TestQuestion record) {
        return testQuestionMapper.insertSelective(record);
    }


    public TestQuestion selectByPrimaryKey(Integer id) {
        return testQuestionMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(TestQuestion record) {
        return testQuestionMapper.updateByPrimaryKeySelective(record);
    }


    public int updateByPrimaryKey(TestQuestion record) {
        return testQuestionMapper.updateByPrimaryKey(record);
    }

    public boolean deleteQuestionById(Integer id) {
        try {
            // 刪除题目
            testQuestionMapper.deleteByPrimaryKey(id);
            // 删除与试卷的关联
            testPaperQuestionMapper.deleteByQuestionId(id);
            // 删除该题目的作答记录
            examAnswerDetailMapper.deleteByQuestionId(id);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 更新试题信息
     */
    @Transactional(rollbackFor = CustomTransactionalException.class)
    public boolean modify(TestQuestion modify) {
        // 删除原有试题
        int delete = testQuestionMapper.deleteByPrimaryKey(modify.getId());
        if (delete != 1) {
            throw new CustomTransactionalException("更新试题信息失败");
        }
        try {
            // 删除与试卷的关联
            testPaperQuestionMapper.deleteByQuestionId(modify.getId());
            // 删除该题目的作答记录
            examAnswerDetailMapper.deleteByQuestionId(modify.getId());
        } catch (Exception e) {
            throw new CustomTransactionalException("更新试题信息失败");
        }
        // 新建试题
        modify.setCreateTime(new Date());
        modify.setId(null);
        int insert1 = testQuestionMapper.insert(modify);
        if (insert1 != 1) {
            throw new CustomTransactionalException("更新试题信息失败");
        }
        return true;
    }

    public PageVo<TestQuestion> pageListQuestion(QuestionPageForm questionPageForm) {
        PageVo<TestQuestion> pageVo = new PageVo<TestQuestion>().setCurrentAndSize(questionPageForm);
        // 设置总的记录条数
        pageVo.setTotal(counterTotalNumber(questionPageForm));
        // 当前查询页数
        int pageIndex = pageVo.getPageIndex();
        // 判断当前页数是否超过总页数
        pageIndex = pageIndex > pageVo.getPages() ? 1 : pageIndex;
        pageVo.setPageIndex(pageIndex);
        // 查询数据
        List<TestQuestion> questions = testQuestionMapper.getPageList((pageIndex - 1) * pageVo.getPageSize(),
                pageVo.getPageSize(),
                questionPageForm.getExamQuestions(),
                questionPageForm.getQuestionType());
        pageVo.setRecords(questions);
        return pageVo;
    }

    private Long counterTotalNumber(QuestionPageForm questionPageForm) {
        return testQuestionMapper.counterTotalNumber(questionPageForm.getExamQuestions(), questionPageForm.getQuestionType());
    }

    public Map<String, List<TestQuestion>> getAllQuestion() {
        Map<String, List<TestQuestion>> map = new HashMap<>(4);
        List<TestQuestion> testQuestionList = testQuestionMapper.getAllQuestion();
        // 1-单选题;2-多选题;3-判断题;4-填空题
        List<TestQuestion> singleChoiceList = new ArrayList<>();
        List<TestQuestion> multipleChoiceList = new ArrayList<>();
        List<TestQuestion> judgeList = new ArrayList<>();
        List<TestQuestion> fillList = new ArrayList<>();
        for (TestQuestion question : testQuestionList) {
            if (question.getType() == 1) {
                singleChoiceList.add(question);
            } else if (question.getType() == 2) {
                multipleChoiceList.add(question);
            } else if (question.getType() == 3) {
                judgeList.add(question);
            } else if (question.getType() == 4) {
                fillList.add(question);
            }
        }
        map.put("singleChoiceList", singleChoiceList);
        map.put("multipleChoiceList", multipleChoiceList);
        map.put("judgeList", judgeList);
        map.put("fillList", fillList);
        return map;
    }

    public TestQuestion getQuestionById(Integer id) {
        return testQuestionMapper.selectByPrimaryKey(id);
    }
}